/*
 * name:密保卡 SerectSecureCard
 * description:校验组件
 * description:密保卡，双文本输入框
 * author:yanmu.wj@taobao.com
 * date:2011-11-23
 */
KISSY.add('SerectSecureCard',function(S,Tip){
    var SerectSecureCard = function(cfg){
        this.input1 = cfg.input1 && S.one(cfg.input1);
        this.input2 = cfg.input2 && S.one(cfg.input2);
        this.checkUrl = cfg.checkUrl || '';
        this.tip = cfg.tip && S.one(cfg.tip) ? Tip(cfg.tip) : null;

        //默认tip信息
        this.defaultTip = cfg.defaultTip || null;

        this.disabledMsg = S.isString(cfg.disabledMsg) ? cfg.disabledMsg : '验证码输入有误，请重新输入！';
        this.disabled = true;
    };
    var _SerectSecureCard = function(cfg){
        return new SerectSecureCard(cfg);
    };
    S.augment(SerectSecureCard,{
        init:function(){
            if(!this.input1 || !this.input2) return this;

            var self = this;
            this.input1.on('keyup blur',function(){
                self.check();
            });
            this.input2.on('keyup blur',function(){
                self.check();
            });

            return this;
        },
        check:function(){
            var self = this;

            //可用性校验
            if(this.checkAble()){
                self.disabled = false;
                self.tip.ok();
            }else{
                self.disabled = true;
                self.tip.error('验证码格式有误，请重新输入！');
            }

            if(!this.checkUrl) return;

            S.io({
                url:self.checkUrl,
                data:{
                    id:S.trim((self.input1.attr('data-id')||'')+(self.input2.attr('data-id'))),
                    code:S.trim(self.input1.val()+self.input2.val())
                },
                type:'post',
                dataType:'json',
                success:function(data){
                    if(data.success){
                        self.disabled = false;
                        self.tip.ok(data.msg || '');
                    }else{
                        self.disabled = true;
                        self.tip.error(data.msg || self.disabledMsg);
                    }
                }
            });
        },
        checkAble:function(){
            var cv1 = !!S.trim(this.input1.val()).match(/^\d{3}$/);
                cv2 = !!S.trim(this.input2.val()).match(/^\d{3}$/);
            return cv1 && cv2;
        },
        reset:function(){
            this.input1 && this.input1.val('');
            this.input2 && this.input2.val('');
            this.tip && this.resetTip();
        },
        resetTip:function(){
            if(!this.tip) return;
            if(this.defaultTip && this.defaultTip.type && this.defaultTip.msg){
                this.tip.change(this.defaultTip.type,this.defaultTip.msg);
            }else{
                this.tip.hide();
            }
        }
    });

    return _SerectSecureCard;
},{
    requires:['Message']
});
